using System;
using System.IO;
using System.Windows.Forms;
using PluginFrame;

namespace PcrExperimentProject
{
	public class FormChooseProjectFromManager : FormProjectManager
	{
		private bool mIsReturnConfirm;

		private string mChoosedProjectFile;

		private IPcrExperimentProject mPcrExperimentProject;

		private IPcrExperimentProject mDefaultProject;

		private IChannelConfiguration[] mChannelConfiguration;

		private IPcrExperiment mPcrExperiment;

		public IPcrExperimentProject PcrExperimentProject
		{
			get
			{
				return mPcrExperimentProject;
			}
		}

		public string ChoosedProjectFile
		{
			get
			{
				return mChoosedProjectFile;
			}
		}

		public bool IsReturnConfirm
		{
			get
			{
				return mIsReturnConfirm;
			}
		}

		public FormChooseProjectFromManager(IApplication application, ref IPcrExperimentProject pcrExperimentProject, IPcrExperimentProject defaultProject, IChannelConfiguration[] channelConfiguration, PcrSystemType systemType, IPcrExperiment pcrExperiment)
			: base(application)
		{
			mPcrExperiment = pcrExperiment;
			btnCreateNewProject.Enabled = defaultProject == null;
			btnConfirm.Visible = true;
			btnConfirm.Text = mResourceManager.GetString("ConfirmString");
			btnClose.Text = mResourceManager.GetString("CancelString");
			mChannelConfiguration = channelConfiguration;
			btnConfirm.Click += btnConfirm_Click;
			base.FormClosing += FormSelectProject_FormClosing;
			mIsReturnConfirm = false;
			mPcrExperimentProject = pcrExperimentProject;
			mDefaultProject = defaultProject;
			ultraProjectFolder.Enabled = false;
			if (mPcrExperimentProject != null)
			{
				mIsAllowToChangeChannelSettings = mPcrExperimentProject.Cells.Count <= 0;
				cmbExperimentType.SelectedValue = pcrExperimentProject.SystemType;
			}
			else
			{
				mIsAllowToChangeChannelSettings = true;
				cmbExperimentType.SelectedValue = systemType;
			}
			Text = mResourceManager.GetString("SelectProjectString");
			cmbExperimentType.Enabled = false;
		}

		protected override void RefreshProjectList()
		{
			string text = string.Empty;
			lstProject.Items.Clear();
			string[] files = Directory.GetFiles(ultraProjectFolder.Text, "*.prj");
			if (files != null)
			{
				bool isNewPcrFile = mPcrExperiment.PcrStructure.PcrDataFile.IsNewPcrFile;
				IChannelConfiguration[] channelConfigurations = mPcrExperiment.PcrStructure.ChannelConfigurations;
				for (int i = 0; i < files.Length; i++)
				{
					IPcrExperimentProject pcrExperimentProject = mMiscService.DeSerializeBinaryFile(files[i]) as IPcrExperimentProject;
					if (pcrExperimentProject == null || pcrExperimentProject.SystemType != (PcrSystemType)cmbExperimentType.SelectedValue)
					{
						continue;
					}
					if (mDefaultProject == null)
					{
						lstProject.Items.Add(Path.GetFileNameWithoutExtension(files[i]));
					}
					else
					{
						if (!pcrExperimentProject.IsProjectMatch(mDefaultProject))
						{
							continue;
						}
						bool flag = true;
						for (int j = 0; j < pcrExperimentProject.ChannelConfigurations.Length; j++)
						{
							if (i <= channelConfigurations.Length)
							{
								if ((!isNewPcrFile || mPcrExperiment.RunningStatus.IsRunning) && pcrExperimentProject.ChannelConfigurations[i].IsAvailable && pcrExperimentProject.ChannelConfigurations[i].IsSelected && !channelConfigurations[i].IsSelected)
								{
									flag = false;
									break;
								}
								if (!pcrExperimentProject.ChannelConfigurations[i].Content.Equals(channelConfigurations[i].Content))
								{
									flag = false;
									break;
								}
							}
						}
						if (flag)
						{
							lstProject.Items.Add(Path.GetFileNameWithoutExtension(files[i]));
							if (pcrExperimentProject.FileName.Equals(mDefaultProject.FileName))
							{
								text = Path.GetFileNameWithoutExtension(files[i]);
							}
						}
					}
				}
			}
			if (string.IsNullOrEmpty(text) && mPcrExperimentProject != null)
			{
				text = Path.GetFileNameWithoutExtension(mPcrExperimentProject.FileName);
			}
			mInitialIndex = lstProject.FindString(text);
			if (mInitialIndex < 0)
			{
				mInitialIndex = 0;
				MessageBox.Show(mResourceManager.GetString("CannotFindProjectFile"), mResourceManager.GetString("ErrorString"));
			}
		}

		protected new void btnConfirm_Click(object sender, EventArgs e)
		{
			try
			{
				mChoosedProjectFile = ultraProjectFolder.Text + lstProject.SelectedItem.ToString() + ".prj";
				mPcrExperimentProject = mPcrExperimentProjectService.LoadExperimentProjectFile(mChoosedProjectFile);
				mIsReturnConfirm = true;
				Close();
			}
			catch (Exception ex)
			{
				mPcrLogService.ShowExceptionDialogBox(ex);
			}
		}

		protected new virtual void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				Close();
				mIsReturnConfirm = false;
			}
			catch (Exception ex)
			{
				mPcrLogService.ShowExceptionDialogBox(ex);
			}
		}

		protected void FormSelectProject_FormClosing(object sender, FormClosingEventArgs e)
		{
		}
	}
}
